<template>
    <div id="Regist">
        <Form ref="formCustom" :model="formCustom" :rules="ruleCustom" :label-width="80">
            <Form-item label="用户名" prop="username">
                <Input class="input" type="text" size="large" v-model="formCustom.username" number></Input>
            </Form-item>
            <Form-item label="密码" prop="password">
                <Input class="input" type="password" size="large" v-model="formCustom.password"></Input>
            </Form-item>
            <Form-item label="确认密码" prop="passwdCheck">
                <Input class="input" type="password" size="large" v-model="formCustom.passwdCheck"></Input>
            </Form-item>
            <Form-item>
                <Button class="input" type="primary" @click="handleSubmit('formCustom')">提交</Button>
            </Form-item>
            <Form-item>
                <Button class="input" type="error" @click="handleReset('formCustom')">重置</Button>
            </Form-item>
        </Form>
    </div>

</template>
<script>
    import axios from 'axios'
    export default {
        name: 'Regist',
        data() {
            const validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.formCustom.passwdCheck !== '') {
                        // 对第二个密码框单独验证
                        this.$refs.formCustom.validateField('passwdCheck');
                    }
                    callback();
                }
            };
            const validatePassCheck = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.formCustom.password) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };

            return {
                formCustom: {
                    username: '',
                    password: '',
                    passwdCheck: '',
                },
                ruleCustom: {
                    username: [
                        { required: true, trigger: 'blur' },
                        { type: 'string', min: 2, message: '用户名不能少于2字', trigger: 'blur' }
                    ],
                    password: [
                        { validator: validatePass, trigger: 'blur' },
                        { type: 'string', min: 6, message: '密码至少6位', trigger: 'blur' }
                    ],
                    passwdCheck: [
                        { validator: validatePassCheck, trigger: 'blur' }
                    ]
                }
            }
        },
        methods: {
            handleSubmit(name) {
                
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        
                        let params={
                            username:this.formCustom.username,
                            password:this.formCustom.password
                        }
                        axios.post("http://localhost:8088/Register",params).then(res=>{
                            console.log(res.data);
                            if(res.data[0].code==200){
                                this.$Message.success('注册成功!');
                                this.$router.replace({path:'/login'});
                            }else{
                                this.$Message.error('注册失败!');
                            }
                        })
                    } else {
                        this.$Message.error('表单验证失败!');
                    }
                })
            },
            handleReset(name) {
                this.$refs[name].resetFields();
            }
        }
    }
</script>

<style>
    #Regist {
        width: 400px;
        margin: 0 auto !important;
    }

    .input {
        width: 400px;
    }
</style>